package com.itheima.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author msk
 * @date 2023/12/17 11:14
 * @Description :
 */
@Component
public class MyInterceptor implements HandlerInterceptor {
    /**
     * 在Controller方法调用之前执行
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("prehandler1()...");
//        System.out.println("User-Agent：" + request.getHeader("User-Agent"));
//        System.out.println("handler class:" + handler.getClass());//org.springframework.web.method.HandlerMethod
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        System.out.println("model and view：" + modelAndView.getClass());
        System.out.println("postHandler1()...");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion1()...");
    }
}
